package tienmanh.gs.goldminer.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.json.JSONException;
import org.json.JSONObject;

import tienmanh.gs.goldminer.manager.ResourcesManager;

import android.util.Log;

public class ParseJson {
	static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
 
    // constructor
    public ParseJson() {
 
    }
 
    public JSONObject getJSONFromUrl() {
 
    	try {
			is = ResourcesManager.getInstance().activity.getAssets().open("file/mapjson.txt");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
         
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "utf-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
//            System.out.println(json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
 
        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
 
        // return JSON String
        return jObj;
 
    }
}
